package com.dykj.fireFighting.web.maintenanceplan;

import java.util.*;

import com.dykj.fireFighting.api.fire.maintenanceplan.dto.RoutineMaintenanceDto;
import com.dykj.fireFighting.api.fire.maintenanceplan.entity.RoutineMaintenance;
import com.dykj.fireFighting.api.fire.maintenanceplan.service.IRoutineMaintenanceService;
import com.dykj.fireFighting.api.fire.maintenanceplan.vo.RoutineMaintenanceNumVo;
import com.dykj.fireFighting.api.fire.maintenanceplan.vo.RoutineMaintenanceVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;



/**
 * 例行维保记录接口
 *
 * @Author zly
 * @Date  2021-07-28 16:30:03
 */
@Slf4j
@Api(value = "例行维保记录接口", tags = {"例行维保记录接口"})
@RestController
@RequestMapping("routineMaintenance")
public class RoutineMaintenanceController {

    @DubboReference
    private IRoutineMaintenanceService routineMaintenanceService;


    /**
     * 例行维保记录列表
     *
     * @return
     */
    @AutoLog(value = "例行维保记录-分页列表查询")
    @ApiOperation(value = "例行维保记录分页列表", notes = "例行维保记录分页列表")
    @GetMapping("/list")
   // @RequiresPermissions("routineMaintenance/list")
    public CommonResult<PageVo<RoutineMaintenanceVo>> treeGrid(RoutineMaintenanceDto routineMaintenanceDto) {

        routineMaintenanceDto.setMaintenanceDept(ShiroUtil.getUser().getEnterpriseId().toString());
        PageVo<RoutineMaintenanceVo> page = routineMaintenanceService.queryList(routineMaintenanceDto);
        return CommonResult.success(page);
    }

    /**
     * 获取例行维保记录信息
     */
    @AutoLog(value = "例行维保记录-获取例行维保记录信息")
    @ApiOperation(value = "获取例行维保记录信息", notes = "获取例行维保记录信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("routineMaintenance/info")
    public CommonResult<RoutineMaintenance> info(@PathVariable("id") String id){
        RoutineMaintenance routineMaintenance = routineMaintenanceService.getById(id);
        return CommonResult.success(routineMaintenance);
    }


    /**
     * 获取例行维保记录信息
     */
    @AutoLog(value = "例行维保记录-获取统计信息")
    @ApiOperation(value = "获取统计信息", notes = "获取统计信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/statistic")
    //@RequiresPermissions("routineMaintenance/info")
    public CommonResult getStatistic(){
        String id =null;
        LoginUserVo loginUserVo=ShiroUtil.getUser();
        if(loginUserVo.getUserType()!=0)
        {
            id=loginUserVo.getEnterpriseId().toString();
        }
        List<RoutineMaintenanceNumVo> list = routineMaintenanceService.getNumStatistic(id);
        return CommonResult.success(list);
    }


    /**
     * 保存例行维保记录信息
     */
    @AutoLog(value = "例行维保记录-保存例行维保记录信息")
    @ApiOperation(value = "保存例行维保记录信息", notes = "保存例行维保记录信息")
    @PostMapping("/save")
    @RequiresPermissions("routineMaintenance/save")
    public CommonResult save(@Valid @RequestBody RoutineMaintenance routineMaintenance) {
        try {
            routineMaintenance.setCreateDate(new Date());
            routineMaintenance.setCreateBy(ShiroUtil.getUser().getId().toString());
                routineMaintenanceService.save(routineMaintenance);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改例行维保记录信息
     */
    @AutoLog(value = "例行维保记录-修改例行维保记录信息")
    @ApiOperation(value = "修改例行维保记录信息", notes = "修改例行维保记录信息")
    @PostMapping("/update")
    @RequiresPermissions("routineMaintenance/update")
    public CommonResult update(@Valid @RequestBody RoutineMaintenance routineMaintenance) {
        try {

                routineMaintenanceService.updateById(routineMaintenance);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除例行维保记录信息
     */
    @AutoLog(value = "例行维保记录-删除例行维保记录信息")
    @ApiOperation(value = "删除例行维保记录信息", notes = "删除例行维保记录信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("routineMaintenance/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                routineMaintenanceService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
